#define _CRT_SECURE_NO_WARNINGS 1

class Solution {
public:
	ListNode* FindFirstCommonNode(ListNode* pHead1, ListNode* pHead2)
	{
		ListNode* curA, * curB;
		curA = pHead1, curB = pHead2;
		int lenA = 1, lenB = 1;
		while (curA != nullptr)
		{
			curA = curA->next;
			++lenA;
		}
		while (curB != nullptr)
		{
			curB = curB->next;
			++lenB;
		}
		if (curA != curB)
			return nullptr;
		ListNode* shortlist = pHead1, * longlist = pHead2;
		int len = abs(lenA - lenB);
		if (lenA > lenB)
		{
			longlist = pHead1;
			shortlist = pHead2;
		}
		while (len--)
		{
			longlist = longlist->next;
		}
		while (longlist != shortlist)
		{
			longlist = longlist->next;
			shortlist = shortlist->next;
		}
		return longlist;
	}
};

